package com.itsu.job.rpc;

import com.itsu.job.exception.JobException;
import com.itsu.job.rpc.basic.EmbedHttpServerHandler;
import com.itsu.job.rpc.request.JobRpcRequestHandler;
import io.netty.channel.ChannelHandler;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Jerry.Su
 * @Date 2022/1/28 11:25
 */
@Slf4j
@Getter
@Setter
@ChannelHandler.Sharable
public class JobRpcServerHandler extends EmbedHttpServerHandler {

    JobRpcRequestHandler stopJobRequestHandler;

    JobRpcRequestHandler scheduleJobRequestHandler;

    JobRpcRequestHandler runJobRequestHandler;

    JobRpcRequestHandler statusJobRequestHandler;

    JobRpcRequestHandler searchJobRequestHandler;

    JobRpcRequestHandler rpcRegisterRequestHandler;

    JobRpcRequestHandler listJobRequestHandler;

//    public JobRpcServerHandler(String accessToken, ThreadPoolExecutor bizThreadPool) {
//        super(accessToken, bizThreadPool);
//    }

    public JobRpcServerHandler(String accessToken, ThreadPoolExecutor bizThreadPool,
                               JobRpcRequestHandler stopJobRequestHandler,
                               JobRpcRequestHandler scheduleJobRequestHandler,
                               JobRpcRequestHandler runJobRequestHandler,
                               JobRpcRequestHandler statusJobRequestHandler,
                               JobRpcRequestHandler allStatusJobRequestHandler,
                               JobRpcRequestHandler rpcRegisterRequestHandler,
                               JobRpcRequestHandler listJobRequestHandler) {
        super(accessToken, bizThreadPool);
        this.stopJobRequestHandler = stopJobRequestHandler;
        this.scheduleJobRequestHandler = scheduleJobRequestHandler;
        this.runJobRequestHandler = runJobRequestHandler;
        this.statusJobRequestHandler = statusJobRequestHandler;
        this.searchJobRequestHandler = allStatusJobRequestHandler;
        this.rpcRegisterRequestHandler = rpcRegisterRequestHandler;
        this.listJobRequestHandler = listJobRequestHandler;
    }

    @Override
    protected Object doDelete(String uri, String requestData) {
        throw new JobException("itsu job only support post http method for rpc call");
    }

    @Override
    protected Object doPut(String uri, String requestData) {
        throw new JobException("itsu job only support post http method for rpc call");
    }

    @Override
    protected Object doPost(String uri, String requestData) {
        Object result = null;
        if ("/stop".equalsIgnoreCase(uri)) {
            result = stopJobRequestHandler.apply(uri, requestData);
        } else if ("/schedule".equalsIgnoreCase(uri)) {
            result = scheduleJobRequestHandler.apply(uri, requestData);
        } else if ("/run".equalsIgnoreCase(uri)) {
            result = runJobRequestHandler.apply(uri, requestData);
        } else if ("/status".equalsIgnoreCase(uri)) {
            result = statusJobRequestHandler.apply(uri, requestData);
        } else if ("/search".equalsIgnoreCase(uri)) {
            result = searchJobRequestHandler.apply(uri, requestData);
        } else if ("/register".equalsIgnoreCase(uri)) {
            result = rpcRegisterRequestHandler.apply(uri, requestData);
        } else if ("/list".equalsIgnoreCase(uri)) {
            result = listJobRequestHandler.apply(uri, requestData);
        } else {
            throw new JobException("not supported rpc request uri: " + uri);
        }
        return result;
    }

    @Override
    protected Object doGet(String uri) {
        throw new JobException("itsu job only support post http method for rpc call");
    }

    @Override
    protected void handleException(Throwable cause) {
        log.error("Job Rpc server occur a exception");
    }
}
